<?php
/**
 *
 */
class ctrl_news extends ctrl_base {
    private static $dirName = 'news';
    
    /**
     * 新闻列表
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('new_list'));
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;
    	
    	$keyword = isset($_POST['keyword']) ? urldecode($_POST['keyword']) : '';
    	$category_id = isset($_POST['category_id'])? intval($_POST['category_id']):0;
    	$search_type = isset($_POST['search_type'])? trim($_POST['search_type']):'title';
		$category_parent_id = isset($_POST['category_parent_id'])? intval($_POST['category_parent_id']):0;
    	$condition = array();
    	if(trim($keyword) != '') {
    		$condition['keyword'] = $keyword;
    		$condition['search_type'] = $search_type;
    	}
    	if($category_id != 0) {
    		$condition['category_id'] = $category_id;
    	}
		if($category_parent_id != 0) {
    		$condition['category_parent_id'] = $category_parent_id;
    	}
    	$news_list = model_news :: newsList($nextpage,$pagesize,$condition);
    	$total = array_pop($news_list);
    	$pageShow = framework_static_common::page(self::$_pagesize, $total, $currentPage);
    	
    	$topic = model_news::getCategoryList();
		$parent_id = empty($category_id)?1:$category_id;
		$child = model_news::get_category_child($parent_id);

		self::smarty() -> assign('child', $child);
    	self::smarty() -> assign('topic', $topic);
    	self::smarty() -> assign('keyword', $keyword);
    	self::smarty() -> assign('wechat_url', WECHAT_HOST);
    	self::smarty() -> assign('pageShow', $pageShow);
    	self::smarty() -> assign('news_list', $news_list);
		self::smarty() -> assign('category_parent_id', $category_parent_id);
		//新添加
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('keyword', $keyword);
		self::smarty() -> assign('category_id', $category_id);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/news_list.html');    	
    }
    
    public static function newsAdd() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('new_add'));
    	if($_POST){
			$info['title'] = trim($_POST['title']);
			$info['author'] = trim($_POST['author']);
			$info['category_id'] = intval($_POST['category_parent_id'])?intval($_POST['category_parent_id']):intval($_POST['category_id']);
			$info['digest'] = trim($_POST['digest']);
			$info['content'] = trim($_POST['content']);
		//	$info['content'] = htmlspecialchars((addslashes($_POST['content'])));
		//	$info['content'] = str_replace('/files/upload/',HOST_URL.'/files/upload/',$info['content']);//上传的图片换成绝对路径
			if(!strpos($info['content'],"http://")) {
				//$info['content'] = str_replace('/files/upload/',HOST_URL.'/files/upload/',$info['content']);//上传的图片换成绝对路径
				$info['content'] = str_replace('/files/upload/',WECHAT_HOST.'/files/upload/',$info['content']);//上传的图片换成绝对路径
			}
			//$info['picurl'] = HOST_URL.trim($_POST['ad_img_path']);
			$info['picurl'] = trim($_POST['ad_img_path']);
			$info['show_cover_pic'] = intval($_POST['show_cover_pic']);
			$info['content_source_url'] = isset($_POST['content_source_url']) ? trim($_POST['content_source_url']) : '';

			$info['article_index'] = intval($_POST['article_index']);
			$info['visible'] = self::handle_category();
			$rs = model_news::newsAdd($info);
		    if($rs <= 0){
	    		self::ajax_ouput(300, '操作失败');
	    	} else {
	    		self::ajax_ouput(200, '操作成功','manger_news','closeCurrent');
	    		exit;
	    	} 
    	}
		$topic = model_news::getCategoryList();
		$category = model_news::get_user_category();
		$child = model_news::get_category_child(1);
		self::smarty() -> assign('child', $child);
		self::smarty() -> assign('category', $category);
		self::smarty() -> assign('topic', $topic);
		self::smarty() -> assign('session', $_SESSION);  
		self::smarty() -> assign('ueditor', microtime(true)*1000);
		
    	self::smarty() -> display(self::$dirName . '/news_add.html');
    	
    }
	//获取下级列表
	public static function get_category(){
		$parent_id = intval($_GET['id']);
		$parent_id = empty($parent_id)?1:$parent_id;
		$child = model_news::get_category_child($parent_id);
		$return = array();
		if(!empty($_GET['list'])){
			$return[0] = array("0",'所有栏目');
		}
		if(!empty($child)){
			foreach($child as $k=>$v){
				$count = count($return);
				$return[$count][] = $v['category_id'];
				$return[$count][] = $v['category_name'];
			}
		}
		die(json_encode($return));
	}
    /**

     * 图片上传

     */

    public static function imageUplody(){
		if(!model_admin::islogin()) {
			self::goto_login();
    	}
    	if($_FILES){
    		$config = array(
    				"savePath" => UPLOAD_FILES_PATH,
    				"maxSize" => 1024,
    				"allowFiles" => array(".png", ".jpg", ".jpeg")
    		);
    		$up = new framework_static_uploader("Filedata", $config);
    		$info = $up->getFileInfo();
    		if($info['state'] != 'SUCCESS'){
    			framework_static_function::output_json($info, false);
    		}
			$info['url'] = str_replace(UPLOAD_FILES_PATH,WECHAT_HOST.'/files/upload/',$info['url']);
    		framework_static_function::output_json($info, false);

    	}

    }
    
	/*
	 * 删除文章
	 */
    public static function newsDelete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('new_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$id[] = intval($v);
		}

    	$st = model_news::delete_news($id);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','manger_news');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
    /*
     * 切换是否显示封面
     */
    public static function newsSetCover() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}

    	$id = isset($_GET['id'])? intval($_GET['id']):0;
    	$show_cover_pic = isset($_GET['show_cover_pic'])? intval($_GET['show_cover_pic']):0;
    	$data['show_cover_pic'] = $show_cover_pic==1? 0:1;

    	$st = model_news::updateNewsInfo($id, $data);

    	if($st >= 0) {

    		framework_static_function::output_json(array('result'=>'ok'));

    	} else {

    		framework_static_function::output_json(array('result'=>'failed'));

    	}
    }
	
    public static function newsEdit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$info = model_news::getNewsInfoById($id);
		$ctrl_tag = 'new_edit_other';
		if($info['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'new_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$info = array();
			$info['title'] = trim($_POST['title']);

			$info['author'] = trim($_POST['author']);

			$info['category_id'] = intval($_POST['category_parent_id'])?intval($_POST['category_parent_id']):intval($_POST['category_id']);

			$info['digest'] = trim($_POST['digest']);

			$info['content'] = trim($_POST['content']);	
			if(!strpos($info['content'],"http://")) {
				//$info['content'] = str_replace('/files/upload/',HOST_URL.'/files/upload/',$info['content']);//上传的图片换成绝对路径
				$info['content'] = str_replace('/files/upload/',WECHAT_HOST.'/files/upload/',$info['content']);//上传的图片换成绝对路径
			}
			$picurl = trim($_POST['ad_img_path']);
			if(strpos($picurl,'http://') === false) {
				$picurl = WECHAT_HOST.$picurl;
			}
			$info['picurl'] = $picurl;

			$info['show_cover_pic'] = intval($_POST['show_cover_pic']);

			$info['content_source_url'] = !empty($_POST['content_source_url']) ? trim($_POST['content_source_url']) : '';

			$info['article_index'] = intval($_POST['article_index']);
			$info['visible'] = self::handle_category();

			$rs = model_news::updateNewsInfo($id, $info);
	    	
		    if($rs >= 0){
				self::ajax_ouput(200, '操作成功','manger_news','closeCurrent');
	    	} else {
				self::ajax_ouput(300, '操作失败');
	    	}    		
    	}
    	$info['content'] = htmlspecialchars_decode((stripslashes($info['content'])));
		if($info['visible'] == '*_*'){
			$info['visible'] = '1_8,1_9,1_10,1_11,1_12,1_13,2_3,2_4,2_5,2_6,2_7';
		}
		$visible = explode(",", $info['visible']);//model_news::get_user_type_of_news($id);
		$info['visible'] = array();
		$info['visibleparents'] = array();
		if(!empty($visible)){
			foreach($visible as $v){
				$arr = explode("_", $v);
				if(!empty($arr[1])){
					$info['visible'][] = $arr[1];//$v['category_id'];
				}
				if(!in_array($arr[0],$info['visibleparents'])){
					$info['visibleparents'][] = $arr[0];
				}
			}
		}
		$topic = model_news::getCategoryList();
		$category = model_news::get_user_category();
		$parent_id = model_news::get_category_parents($info['category_id']);
		$info['category_parent_id'] = $parent_id;
		$child = array();
		if(!empty($parent_id)){
			$child = model_news::get_category_child($parent_id);
		}
		self::smarty() -> assign('child', $child);
		self::smarty() -> assign('category', $category);
		self::smarty() -> assign('topic', $topic);
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('info', $info);
		self::smarty() -> assign('session', $_SESSION);
		self::smarty() -> assign('ueditor', microtime(true)*1000);
    	self::smarty() -> display(self::$dirName . '/news_edit.html');
    }
}
